#include "Expand/Plugins/BanPlugin.h"
#include "Expand/PluginsManager.h"

#include "Log.h"
#include "Network/Client.h"
#include "Player/PlayerData.h"
#include "Subsystems/SubsystemPlayers.h"

#include "GameSingleton.h"
#include "Network/PackageManager.h"
#include "Network/Packages/MessagePackage.h"
#include "Network/BanManager.h"

namespace PocketSurvival
{
    const std::string &BanPlugin::getPluginName()
    {
        static const std::string pluginName = "封号插件";
        return pluginName;
    }

    const std::string &BanPlugin::getIntroduce()
    {
        static const std::string introduce = "\n/ban help - 查看封禁指令用法";
        return introduce;
    }

    uint32_t BanPlugin::getUseAuthLevel()
    {
        return 100;
    }

    AuthDisplayType BanPlugin::getDisplayType()
    {
        return AuthDisplayType::Authority;
    }

    void BanPlugin::processCmd(ChatObj &chatObj, std::vector<std::string> &cmdDataVec)
    {
        if(cmdDataVec.size() >= 3)
        {
            if(cmdDataVec[1] == "user")
            {
                std::vector<std::shared_ptr<PlayerData>> playerVec;
                GameSingleton::singletonPtr->getSingleton<SubsystemPlayers>()->getPlayerDataByName(cmdDataVec[2], playerVec);
                if(playerVec.size() == 0)
                {
                    chatObj.sendMessage("ban", fmt::format("玩家 {} 貌似不在线", cmdDataVec[2]));
                }
                else
                {
                    for(std::shared_ptr<PlayerData> &playerData : playerVec)
                    {
                        GameSingleton::banManager->addBanGuid(playerData->playerGuid);
                    }
                    chatObj.sendMessage("ban", fmt::format("成功封禁所有名字叫 {} 的玩家", cmdDataVec[2]));
                }
                return;
            }
            else if(cmdDataVec[1] == "guid")
            {
                GameGuid guid(cmdDataVec[2]);
                GameSingleton::banManager->addBanGuid(guid);
                chatObj.sendMessage("ban", fmt::format("成功封禁GUID: {}",guid.toString()));
                return;
            }
            else if(cmdDataVec[1] == "ip")
            {
                GameSingleton::banManager->addBanIp(cmdDataVec[2]);
                chatObj.sendMessage("ban", fmt::format("成功封禁IP: {}", cmdDataVec[2]));
                return;
            }
            else if(cmdDataVec[1] == "rguid")
            {
                GameGuid guid(cmdDataVec[2]);
                if(GameSingleton::banManager->removeBanGuid(guid))
                {
                    chatObj.sendMessage("ban", fmt::format("成功解禁GUID: {}", guid.toString()));
                }
                else
                {
                    chatObj.sendMessage("ban", fmt::format("GUID: {} ，没有在封禁里面", guid.toString()));
                }
                return;
            }
            else if(cmdDataVec[1] == "rip")
            {
                if(GameSingleton::banManager->removeBanIp(cmdDataVec[2]))
                {
                    chatObj.sendMessage("ban", fmt::format("成功解禁IP: {}", cmdDataVec[2]));
                }
                else
                {
                    chatObj.sendMessage("ban", fmt::format("IP: {} ，没有在封禁里面", cmdDataVec[2]));
                }
                return;
            }
        }
        chatObj.sendMessage("ban", "\n/ban user (在线玩家) - 封禁该玩家\n/ban guid (玩家GUID) - 封禁该玩家\n/ban ip (ip地址) - 封禁该IP\n/ban rguid (玩家GUID) - 解禁该玩家\n/ban rip (ip地址) - 解禁该ip");
        return;
    }

    void BanPlugin::getCmds(std::vector<std::string> &cmdVec)
    {
        cmdVec.push_back("/ban");
    }

    void BanPlugin::load() {}

} // namespace PocketSurvival
